package com.ys.utils.yinsheng.yztdock.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 日期时间工具类
 * @author Administrator
 */
public class DateUtil {

    public static final SimpleDateFormat YEAR = new SimpleDateFormat("yyyy");
    public static final SimpleDateFormat DAY = new SimpleDateFormat(
            "yyyy-MM-dd");
    public static final SimpleDateFormat DAY4 = new SimpleDateFormat(
            "yyyy年MM月dd日");
    public static final SimpleDateFormat DAY3 = new SimpleDateFormat("yyyyMMdd");
    public static final SimpleDateFormat DAY2 = new SimpleDateFormat(
            "yyyy/MM/dd");
    public static final SimpleDateFormat DAYTIME = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss");
    public static final SimpleDateFormat DAYTIME2 = new SimpleDateFormat(
            "yyyy/MM/dd HH:mm:ss");
    public static final SimpleDateFormat DAYTIME3 = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm");
    public static final SimpleDateFormat DAYTIME4 = new SimpleDateFormat(
            "MMddHHmmss");
    public static final SimpleDateFormat DAYTIME5 = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss.S");
    public static final SimpleDateFormat MICROSECONDS = new SimpleDateFormat(
            "yyyyMMddHHmmssSSS");
    public static final SimpleDateFormat MICROSECONDSID = new SimpleDateFormat(
            "yyMMddHHmmssSSS");
    public static final SimpleDateFormat SMALLMICROSECONDS = new SimpleDateFormat(
            "yyMMddHHmmss");
    public static final SimpleDateFormat MIDDLEMICROSECONDS = new SimpleDateFormat(
            "yyyyMMddHHmmss");
    public static final SimpleDateFormat SMALLMIHHMMSS = new SimpleDateFormat(
            "HHmmss");
    public static final SimpleDateFormat SMALLMIHHMMSS2 = new SimpleDateFormat(
            "HH:mm:ss");
    public static final SimpleDateFormat DIAN = new SimpleDateFormat(
            "yyyy.MM.dd");
    public static final SimpleDateFormat DAY5 = new SimpleDateFormat(
            "yyyy年MM月dd日 HH:mm");
    // 月份
    public static final SimpleDateFormat Month = new SimpleDateFormat("yyyy-MM");
    public static final SimpleDateFormat Month1 = new SimpleDateFormat("yyyy年MM月");


    /**
     * 获得时间差<分>
     *
     * @param start
     * @param end
     * @return
     */
    public static Integer geTimes(Date start, Date end) {
        Long timeLongs = end.getTime() - start.getTime();
        Double times = (double) (timeLongs / (1000 * 60));
        return times.intValue();
    }

    /**
     * 格式化当前日期字符串，返回yyyyMMdd 
     * @return
     */
    public static String getCurrentDate(){
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return sdf.format(new Date());
    }
    
    /**
     * 获取昨天的日期，返回yyyyMMdd 
     * @return
     */
    public static String getLastDate(){
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    	Calendar calendar = Calendar.getInstance();
    	calendar.setTime(new Date());
    	calendar.add(Calendar.DAY_OF_YEAR,-1);
        return sdf.format(calendar.getTime());
    }    
    
    /**
     * 格式化当前时间字符串，返回HHmmssSSSsss 
     * @return
     */
    public static String getCurrentTime(){
    	SimpleDateFormat sdf = new SimpleDateFormat("HHmmssSSSsss");
        return sdf.format(new Date());
    }
    /**
     * 格式化当前时间字符串，返回指定格式如：style=HHmmss 
     * @return
     */
    public static String getCurrentTime(String style){
    	SimpleDateFormat sdf = new SimpleDateFormat(style);
        return sdf.format(new Date());
    }
    
    /**
     * 格式化当前日期时间字符串，返回yyyyMMddHHmmssSSS
     * @return
     */
    public static String getCurrentDateTime(){
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return sdf.format(new Date());
    }    
    
    /**
     * 格式化当前日期时间字符串
     * @return
     */
    public static String getCurrentDateTime(String pattern){
    	SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(new Date());
    }
    
    /**
     * 格式化指定时间和指定格式字符串 
     * @return
     */
    public static String formatDate(Date date, String pattern){
    	SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }
    
    /**
     * 把字符串按照指定格式转为日期时间 
     * @return
     */
    public static Date toDate(String dateStr, String pattern) throws Exception {
    	SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.parse(dateStr);
    }     
    
    /**
     * 
     * @Title: getMonthBegin 
     * @Description: 根据月份得到开始日期
     * @param month
     * @return 
     * @return String 
     * @throws
     */
    public static String getMonthBegin(String month)
    {
        return month + "01";
    }
    /**
     * 
     * @Title: getMonthEnd 
     * @Description: 根据日期得到结束日期
     * @param month
     * @return
     * @throws Exception
     * @return String 
     * @throws
     */
    public static String getMonthEnd(String month) throws Exception
    {
        Date date = toDate(getMonthBegin(month), "yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH,1);
        calendar.add(Calendar.DAY_OF_YEAR, -1);
        return formatDate(calendar.getTime(),"yyyyMMdd");
    }
    /**
     * 得到月份
     * @Title: getMonth
     * @Description: 得到月份
     * @param date
     * @return
     * @return String
     * @throws
     */
    public static String getMonth(Date date){
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
    	return sdf.format(date);
    }
    /**
     * <p>
     * 取系统当前时间
     * </p>
     *
     * @return 返回系统当前时间(java.util.Date类型 0000-00-00)
     */
    public static final Date getTodayDate() {
        long date = System.currentTimeMillis();
        return new Date(date);
    }
    /**
     * <p>
     * 创建时间
     * </p>
     *
     * @param year
     *            "2007" 形式的字符串
     * @param month
     *            "09" 形式的字符串
     * @param date
     *            "05" 形式的字符串
     * @return 返回"2007-09-05"格式化的时间(java.util.Date)
     */
    public static final Date getDate(String year, String month, String date) {

        int day = Integer.parseInt(date);
        int monthT = Integer.parseInt(month);
        int yearT = Integer.parseInt(year);
        Calendar rightNow = Calendar.getInstance();
        rightNow.set(yearT, monthT - 1, day);
        return new Date(rightNow.getTime().getTime());

    }
    private static int getYearPlus(){
        Calendar cd = Calendar.getInstance();
        int yearOfNumber = cd.get(Calendar.DAY_OF_YEAR);//获得当天是一年中的第几天
        cd.set(Calendar.DAY_OF_YEAR,1);//把日期设为当年第一天
        cd.roll(Calendar.DAY_OF_YEAR,-1);//把日期回滚一天。
        int maxYear = cd.get(Calendar.DAY_OF_YEAR);
        if(yearOfNumber == 1){
            return -maxYear;
        }else{
            return 1-yearOfNumber;
        }
    }
  //获得本年第一天的日期    YYYY 1月1日
    public static Date getCurrentYearFirst(){
        int yearPlus = getYearPlus();
        GregorianCalendar currentDate = new GregorianCalendar();
        currentDate.add(GregorianCalendar.DATE,yearPlus);
       // Date yearDay = currentDate.getTime();
        return currentDate.getTime();
    }
    
    //计算订单失效时间
	public static Date countExpireDate(String min){
		Integer minNum = Integer.valueOf(min);
		Calendar nowTime = Calendar.getInstance();
		nowTime.add(Calendar.MINUTE, minNum);
		return nowTime.getTime();
	}

    /**
     *
     *
     * @author 周杰伦 2016-12-9
     *
     */
    public enum FormatType {
        /** yyyy, curdate() */
        YEAR,
        /** yyyy-MM-dd, curdate() */
        DAY,
        /** yyyy-MM-dd HH:mm:ss, now() */
        DAYTIME,
        /** yyyy-MM-dd HH:mm, now() */
        DAYTIME3,
        /** yyyyMMddHHmmssSSS, 17位毫秒时间序列号 */
        MICROSECONDS,
        /** yyyyMMddHHmmss, 14位毫秒时间序列号 */
        MIDDLEMICROSECONDS,
        /** yyMMddHHmmss, 12位毫秒时间序列号 */
        SMALLMICROSECONDS,
        /** 1406167122870, System.currentTimeInMillis() */
        JAVA,
        /** 1406166160, unix_timestamp(now( )) */
        MYSQL,
        /** yyyyMMdd */
        DAY3,
        /** yyyy年MM月dd日 */
        DAY4,
        /** HHMMSS */
        SMALLMIHHMMSS,
        /** HH:MM:SS */
        SMALLMIHHMMSS2,
        /** MMddhhmmss */
        DAYTIME4,
        /** yyyy.MM.dd */
        DIAN,
        /** yyyy年MM月dd日 HH：mm */
        DAY5,
        /** yyyy-MM */
        Month,
        /** yyyy年MM月 */
        Month1,
        /** yyMMddHHmmssSSS */
        MICROSECONDSID
    };

    /**
     * @param type
     *            格式化日期<li>DAY yyyy-MM-dd, mysql: curdate()</li> <li>DAYTIME
     *            yyyy-MM-dd HH:mm:ss, mysql: now()</li> <li>JAVA
     *            1406167122870，java: System.currentTimeInMillis()</li> <li>
     *            MYSQL 1406166160，mysql: unix_timestamp(now())</li>
     */
    public static String format(Date date, FormatType type) {
        if (date == null) {
            return null;
        }
        final int a = 1000;
        switch (type) {
            case YEAR:
                synchronized (YEAR) {
                    return YEAR.format(date);
                }
            case DAY:
                synchronized (DAY) {
                    return DAY.format(date);
                }
            case DAYTIME:
                synchronized (DAYTIME) {
                    return DAYTIME.format(date);
                }
            case DAYTIME3:
                synchronized (DAYTIME3) {
                    return DAYTIME3.format(date);
                }
            case DAYTIME4:
                synchronized (DAYTIME4) {
                    return DAYTIME4.format(date);
                }
            case MICROSECONDS:
                synchronized (MICROSECONDS) {
                    return MICROSECONDS.format(date);
                }
            case SMALLMICROSECONDS:
                synchronized (SMALLMICROSECONDS) {
                    return SMALLMICROSECONDS.format(date);
                }
            case JAVA:
                return String.valueOf(date.getTime());
            case MYSQL:
                return String.valueOf(date.getTime() / a);
            case DAY3:
                synchronized (DAY3) {
                    return DAY3.format(date);
                }
            case DAY4:
                synchronized (DAY4) {
                    return DAY4.format(date);
                }
            case SMALLMIHHMMSS:
                synchronized (SMALLMIHHMMSS) {
                    return SMALLMIHHMMSS.format(date);
                }
            case SMALLMIHHMMSS2:
                synchronized (SMALLMIHHMMSS2) {
                    return SMALLMIHHMMSS2.format(date);
                }
            case MIDDLEMICROSECONDS:
                synchronized (MIDDLEMICROSECONDS) {
                    return MIDDLEMICROSECONDS.format(date);
                }
            case DIAN:
                synchronized (DIAN) {
                    return DIAN.format(date);
                }
            case DAY5:
                synchronized (DAY5) {
                    return DAY5.format(date);
                }
            case Month:
                synchronized (Month) {
                    return Month.format(date);
                }
            case Month1:
                synchronized (Month1) {
                    return Month1.format(date);
                }
            case MICROSECONDSID:
                synchronized (MICROSECONDSID) {
                    return MICROSECONDSID.format(date);
                }
        }
        return null;
    }

}
